package org.jeecg.modules.water.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basic.demo.apartment.entity.LwgyBaseApartment;
import org.jeecg.modules.basic.demo.apartment.service.ILwgyBaseApartmentService;
import org.jeecg.modules.water.entity.LwgyJointWaterCmd;
import org.jeecg.modules.water.entity.LwgyJointWaterDaily;
import org.jeecg.modules.water.mapper.LwgyJointWaterCmdMapper;
import org.jeecg.modules.water.service.ILwgyJointWaterCmdService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 阀控记录
 * @Author: jeecg-boot
 * @Date: 2024-03-11
 * @Version: V1.0
 */
@Api(tags = "阀控记录")
@RestController
@RequestMapping("/water/lwgyJointWaterCmd")
@Slf4j
public class LwgyJointWaterCmdController extends JeecgController<LwgyJointWaterCmd, ILwgyJointWaterCmdService> {
    @Autowired
    private ILwgyJointWaterCmdService lwgyJointWaterCmdService;

    @Autowired
    private ILwgyBaseApartmentService lwgyBaseApartmentService;

    @Autowired
    private LwgyJointWaterCmdMapper lwgyJointWaterCmdMapper;
    /**
     * 分页列表查询
     *
     * @param lwgyJointWaterCmd
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "阀控记录-分页列表查询")
    @ApiOperation(value = "阀控记录-分页列表查询", notes = "阀控记录-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(LwgyJointWaterCmd lwgyJointWaterCmd,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        String type = null;
        String id = lwgyJointWaterCmd.getRoomId();
        if (oConvertUtils.isNotEmpty(id)) {
            LwgyBaseApartment lwgyBaseApartment = lwgyBaseApartmentService.getById(id);
            type = lwgyBaseApartment.getType();
        }
        QueryWrapper<LwgyJointWaterCmd> queryWrapper = QueryGenerator.initQueryWrapper(lwgyJointWaterCmd, req.getParameterMap());
        if (id != null && type != null) {
            switch (type) {
                case "campus":
                    queryWrapper.eq("t.campus_id", id);
                    break;
                case "building":
                    queryWrapper.eq("t.building_id", id);
                    break;
                case "unit":
                    queryWrapper.eq("t.unit_id", id);
                    break;
                case "layer":
                    queryWrapper.eq("t.layer_id", id);
                    break;
                case "room":
                    queryWrapper.eq("t.room_id", id);
                    break;
                default:
                    break;
            }
        }
        queryWrapper.orderByDesc("create_time");
        Page<LwgyJointWaterCmd> page = new Page<LwgyJointWaterCmd>(pageNo, pageSize);
        IPage<LwgyJointWaterCmd> pageList = lwgyJointWaterCmdService.queryJointWaterCmdList(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param lwgyJointWaterCmd
     * @return
     */
    @AutoLog(value = "阀控记录-添加")
    @ApiOperation(value = "阀控记录-添加", notes = "阀控记录-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody LwgyJointWaterCmd lwgyJointWaterCmd) {
        lwgyJointWaterCmdService.save(lwgyJointWaterCmd);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param lwgyJointWaterCmd
     * @return
     */
    @AutoLog(value = "阀控记录-编辑")
    @ApiOperation(value = "阀控记录-编辑", notes = "阀控记录-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody LwgyJointWaterCmd lwgyJointWaterCmd) {
        lwgyJointWaterCmdService.updateById(lwgyJointWaterCmd);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "阀控记录-通过id删除")
    @ApiOperation(value = "阀控记录-通过id删除", notes = "阀控记录-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        lwgyJointWaterCmdService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "阀控记录-批量删除")
    @ApiOperation(value = "阀控记录-批量删除", notes = "阀控记录-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.lwgyJointWaterCmdService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "阀控记录-通过id查询")
    @ApiOperation(value = "阀控记录-通过id查询", notes = "阀控记录-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        LwgyJointWaterCmd lwgyJointWaterCmd = lwgyJointWaterCmdService.getById(id);
        if (lwgyJointWaterCmd == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(lwgyJointWaterCmd);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param lwgyJointWaterCmd
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LwgyJointWaterCmd lwgyJointWaterCmd) {
        // Step.1 组装查询条件
        QueryWrapper<LwgyJointWaterCmd> queryWrapper = QueryGenerator.initQueryWrapper(lwgyJointWaterCmd, request.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String type = null;
        String id = lwgyJointWaterCmd.getRoomId();
        if (oConvertUtils.isNotEmpty(id)) {
            LwgyBaseApartment lwgyBaseApartment = lwgyBaseApartmentService.getById(id);
            type = lwgyBaseApartment.getType();
        }
        if (id != null && type != null) {
            switch (type) {
                case "campus":
                    queryWrapper.eq("t.campus_id", id);
                    break;
                case "building":
                    queryWrapper.eq("t.building_id", id);
                    break;
                case "unit":
                    queryWrapper.eq("t.unit_id", id);
                    break;
                case "layer":
                    queryWrapper.eq("t.layer_id", id);
                    break;
                case "room":
                    queryWrapper.eq("t.room_id", id);
                    break;
                default:
                    break;
            }
        }
        queryWrapper.orderByDesc("create_time");
        Page<LwgyJointWaterCmd> page = new Page<LwgyJointWaterCmd>(0, 100000);
        List<LwgyJointWaterCmd> pageList = lwgyJointWaterCmdMapper.queryJointWaterCmdList(page, queryWrapper);

        List<LwgyJointWaterCmd> exportList = null;
        // 过滤选中数据
        String selections = request.getParameter("selections");
        if (oConvertUtils.isNotEmpty(selections)) {
            List<String> selectionList = Arrays.asList(selections.split(","));
            exportList = pageList.stream().filter(item -> selectionList.contains(getId(item))).collect(Collectors.toList());
        } else {
            exportList = pageList;
        }

        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, "阀控记录"); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, LwgyJointWaterCmd.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("阀控记录" + "报表", "导出人:" + sysUser.getRealname(), "阀控记录"));
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LwgyJointWaterCmd.class);
    }


}
